<template>
    <view class="content">
		<br/><br/><br/><br/><br/>
        <view class="input-group">
            <view class="input-row border">
                <text class="title">账号：</text>
                <m-input type="text" focus clearable v-model="account" placeholder="请输入账号"></m-input>
            </view>
            <view class="input-row border">
                <text class="title">密码：</text>
                <m-input type="password" displayable v-model="password" placeholder="请输入密码"></m-input>
            </view>
            <view class="input-row">
                <text class="title">邮箱：</text>
                <m-input type="text" clearable v-model="email" placeholder="请输入邮箱"></m-input>
            </view>
        </view>
        <view class="btn-row">
            <button type="primary" class="primary" @tap="register">下一步</button>
        </view>
    </view>
</template>

<script>
    import service from '../../service.js';
    import mInput from '../../components/m-input.vue';

    export default {
        components: {
            mInput
        },
        data() {
            return {
                account: '',
                password: '',
                email: ''
            }
        },
        methods: {
            register() {
                /**
                 * 客户端对账号信息进行一些必要的校验。
                 * 实际开发中，根据业务需要进行处理，这里仅做示例。
                 */
                if (this.account.length < 3 || this.account.length > 15) {
                    uni.showToast({
                        icon: 'none',
                        title: '账号长度为 3-15 个字符'
                    });
                    return;
                }
                if (this.password.length < 6 || this.password.length > 20) {
                    uni.showToast({
                        icon: 'none',
                        title: '密码长度为 6-20 个字符'
                    });
                    return;
                }
                if (this.email.length < 3 || !~this.email.indexOf('@')) {
                    uni.showToast({
                        icon: 'none',
                        title: '邮箱地址不合法'
                    });
                    return;
                }
				
                const data = {
                    account: this.account,
                    password: this.password,
                    email: this.email
                }
				//检验请求
				this.request({
					url: `user/checkAccountPwdEmail`,
					params: data,
					method: 'POST'
				}).then(res => {
					// alert(JSON.stringify(res))
					console.log(res)
					if(res.data.msg=="账号已注册"){
						uni.showToast({
							icon: 'none',
						    title: '账号已注册'
						});
					}
					if(res.data.msg=="邮箱已注册"){
						uni.showToast({
							icon: 'none',
						    title: '邮箱已注册'
						});
					}
					if(res.data.msg=="注册成功"){
						service.addUser(data);
						uni.showToast({
						    title: '注册成功'
						});
						//回退一页
						// uni.navigateBack({
						//     delta: 1
						// });
						uni.navigateTo({
							url: "/pages/user/identityAuth"
						})
					}
				}).catch(err => {
					console.log(err)
					// alert(JSON.stringify(err))
				})
            }
        }
    }
</script>

<style>

</style>
